package MyDaily.Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import MyDaily.dao.daily.InsertDaily;
import MyDaily.pojo.Daily;

/**
 * Servlet implementation class keepDiaryServlet
 */
@WebServlet("/keepDiaryServlet")
public class keepDiaryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
//	public keepDiaryServlet() {
//		super();
//		// TODO Auto-generated constructor stub
//	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 设置请求和响应数据的字符集编码
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		// 获取用户输入的日记、类型和系统时间
		// 从session中获取用户名
		HttpSession session = request.getSession();
		String name = session.getAttribute("name").toString();
		String daily = request.getParameter("text");
		String type = request.getParameter("type");
		// 获取系统时间
		Date date1 = new Date();
		java.sql.Timestamp date = new java.sql.Timestamp(date1.getTime());
		//初始化Daily
		Daily daliy = new Daily();
		daliy.setName(name);
		daliy.setDiary(daily);
		daliy.setType(type);
		daliy.setDate(date);
		//调用新增日记方法
		InsertDaily insertDaily = new InsertDaily();
		int row = insertDaily.Inserdaily(daliy);
		if (row > 0) {
			//弹窗
//			PrintWriter out= response.getWriter();
//			out.print("<script>window.parent.confirm('记录 日记成功，在我的日记查看')</script>");
//			//必须调用
//			out.flush();
//			//重定向
//			response.sendRedirect("home.jsp");
			request.setAttribute("msg", "添加日记成功，请在我的日记查阅");
			request.getRequestDispatcher("home.jsp").forward(request, response);
		}
		
	}

}
